/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mapbox.ohos.gestures;


import com.mapbox.ohos.gestures.ohan.PointF;

import ohos.agp.components.AttrHelper;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 工具类
 *
 * @since 20212-07-28
 */
public class Utils {
    /**
     * Calculates the center point of the multi finger gesture.
     *
     * @param motionEvent event
     * @return center point of the gesture
     */
    public static PointF determineFocalPoint(TouchEvent motionEvent) {
        int pointersCount = motionEvent.getPointerCount();
        float pointerX = 0;
        float pointerY = 0;

        for (int i = 0; i < pointersCount; i++) {
            pointerX += motionEvent.getPointerPosition(i).getX();
            pointerY += motionEvent.getPointerPosition(i).getY();
        }

        return new PointF(pointerX / pointersCount, pointerY / pointersCount);
    }

    /**
     * getRawX
     *
     * @param event motion event
     * @param pointerIndex pointer's index
     * @return rawY for a pointer
     * @author Almer Thie (code.almeros.com)
     * <p>
     * MotionEvent has no getRawY(int) method; simulate it pending future API approval.
     */
    public static float getRawX(TouchEvent event, int pointerIndex) {
        float offset = event.getPointerScreenPosition(pointerIndex).getX() - event.getPointerPosition(pointerIndex).getX();
        if (pointerIndex < event.getPointerCount()) {
            return event.getPointerPosition(pointerIndex).getX() + offset;
        }
        return 0.0f;
    }

    /**
     * getRawY
     *
     * @param event motion event
     * @param pointerIndex pointer's index
     * @return rawX for a pointer
     * @author Almer Thie (code.almeros.com)
     * <p>
     * MotionEvent has no getRawX(int) method; simulate it pending future API approval.
     */
    public static float getRawY(TouchEvent event, int pointerIndex) {
        float offset = event.getPointerScreenPosition(pointerIndex).getY() - event.getPointerPosition(pointerIndex).getY();
        if (pointerIndex < event.getPointerCount()) {
            return event.getPointerPosition(pointerIndex).getY() + offset;
        }
        return 0.0f;
    }

    /**
     * Converts FP to PX.
     *
     * @param fp initial value
     * @param context context
     * @return converted value
     */
    public static float fpToPx(float fp, Context context) {
        return AttrHelper.fp2px(fp, context);
    }

}


